package com.bruce.spring.demo;

import com.bruce.spring.demo.config.HelloBeanPostProcessor;
import com.bruce.spring.demo.entity.ApplicationDemoUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanDefinitionCustomizer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author luotuan
 * @Description
 * @create 2021-04-27 14:08
 **/
@Slf4j
public class AnnotationConfigApplicationContextDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.registerBean("helloUser", ApplicationDemoUser.class, () -> {
            ApplicationDemoUser user = new ApplicationDemoUser();
            user.setAge(11);
            user.setName("hello");
            return user;
        }, (BeanDefinitionCustomizer) beanDefinition -> beanDefinition.setPrimary(true));

        applicationContext.registerBean(ApplicationDemoUser.class, () -> {
            ApplicationDemoUser user = new ApplicationDemoUser();
            user.setAge(11);
            user.setName("hello");
            return user;
        });
        applicationContext.getBeanFactory().addBeanPostProcessor(new HelloBeanPostProcessor());
        applicationContext.refresh();
        ApplicationDemoUser user = applicationContext.getBean(ApplicationDemoUser.class);
        log.info("user------"+user);
        applicationContext.close();
    }
}
